package com.longshihan.carservice.service.goods;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.longshihan.carservice.dao.GoodsDao;
import com.longshihan.carservice.dao.LogDao;
import com.longshihan.carservice.model.GoodsDomain;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsDao goodsDao;

    @Override
    public int addGoodsBean(GoodsDomain goodsDomain) {
        return goodsDao.addGoods(goodsDomain);
    }


    @Override
    public int addGoods(String goodsId,String goodsName,String des,
                        String authorId,String type,String status,
                        double price, double originprice,
                        String province,String goodsImage,String tags,
                        String categories) {
        GoodsDomain goodsDomain=new GoodsDomain();
        goodsDomain.setGoodsId(goodsId);
        goodsDomain.setAuthorId(authorId);
        goodsDomain.setTitle(goodsName);
        goodsDomain.setDesPic(goodsImage);
        goodsDomain.setContent(des);
        goodsDomain.setMoney(price);
        goodsDomain.setType(type);
        goodsDomain.setStatus(status);
        goodsDomain.setOriginprice(originprice);
        goodsDomain.setProvince(province);
        goodsDomain.setTags(tags);
        goodsDomain.setCategories(categories);
        return addGoodsBean(goodsDomain);
    }

    @Override
    public int deleteGoodsById(String goodsId) {
        return goodsDao.deleteGoodsById(goodsId);
    }

    @Override
    public int updateGoodsBean(GoodsDomain goodsDomain) {
        return goodsDao.updateGoods(goodsDomain);
    }

    @Override
    public int updateGoods(String authorId,String goodsId,String goodsName,String des,
                           String type,String status,
                           double price, double originprice,
                           String province,String goodsImage,String tags,
                           String categories) {
        GoodsDomain goodsDomain=goodsDao.getGoodsById(goodsId,authorId);
        if (goodsDomain==null){
            return 0;
        }else {
            goodsDomain.setGoodsId(goodsId);
            goodsDomain.setTitle(goodsName);
            goodsDomain.setDesPic(goodsImage);
            goodsDomain.setContent(des);
            goodsDomain.setMoney(price);
            goodsDomain.setType(type);
            goodsDomain.setStatus(status);
            goodsDomain.setOriginprice(originprice);
            goodsDomain.setProvince(province);
            goodsDomain.setTags(tags);
            goodsDomain.setCategories(categories);
            return updateGoodsBean(goodsDomain);
        }
    }

    @Override
    public PageInfo<GoodsDomain> getAuthorGoods(String authorId, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<GoodsDomain> goodsDomains = goodsDao.getAuthorGoods(authorId);
        PageInfo<GoodsDomain> pageInfo = new PageInfo<>(goodsDomains);
        return pageInfo;
    }

    @Override
    public PageInfo<GoodsDomain> getGoods(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<GoodsDomain> goodsDomains = goodsDao.getGoods();
        PageInfo<GoodsDomain> pageInfo = new PageInfo<>(goodsDomains);
        return pageInfo;
    }

    @Override
    public GoodsDomain getGoodsById(String goodsId,String authorId) {
        return goodsDao.getGoodsById(goodsId,authorId);
    }
}
